/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.test_factory;

import picounit.features.instrumentation.InstrumentationController;

import java.lang.reflect.Method;

// TODO: Allow more than one TestListener
class InstrumentationUpdatingTestListener implements TestListener {
	private final InstrumentationController instrumentationController;

	private int numTests = 0;

	private int currentTestIndex = 0;

	public InstrumentationUpdatingTestListener(InstrumentationController instrumentationController) {
		this.instrumentationController = instrumentationController;
	}

	public void testPrepared() {
		numTests++ ;
	}

	public void testStarted(String testClass, String testName) {
		instrumentationController.incrementTests();
		instrumentationController.setCurrentTest(testClass, testName);

		if (currentTestIndex == 0) {
			instrumentationController.setIsFirst(true);
			setIsLast(false);
		}
		else if (currentTestIndex == 1) {
			instrumentationController.setIsFirst(false);
		}
		if (currentTestIndex == (numTests - 1)) {
			setIsLast(true);
		}

		currentTestIndex++ ;
	}

	public void testFinished(String testClass, String testName) {
	}

	public void addFailure(Class testClass, Method testMethod) {
		instrumentationController.addFailure(testClass, testMethod);
	}

	public void addError(Class testClass, Method testMethod) {
		instrumentationController.addError(testClass, testMethod);
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// Implementation methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	private void setIsLast(boolean isLast) {
		instrumentationController.setIsLast(isLast);
	}
}